#include "comm.h"
#include "stdlib.h"

void little16_to_str(rt_uint16_t value,rt_uint8_t *str)
{
	str[1] = (value & 0xff00)>>8;
	str[0] = (value & 0x00ff);
}

void uint16_to_str(rt_uint16_t value,rt_uint8_t *str)
{
	str[0] = (value & 0xff00)>>8;
	str[1] = (value & 0x00ff);
}

void uint32_to_str(rt_uint32_t value,rt_uint8_t *str)
{
	str[0] = (value & 0xff000000)>>24;
	str[1] = (value & 0x00ff0000)>>16;
	str[2] = (value & 0x0000ff00)>>8;
	str[3] = (value & 0x000000ff);
}

rt_uint16_t str_to_uint16(rt_uint8_t *str)
{
	rt_uint16_t value = 0;

	value = str[0];
	value <<= 8;
	value |= str[1];

	return value;
}

rt_uint16_t str_to_little16(rt_uint8_t *str)
{
	rt_uint16_t value = 0;

	value = str[1];
	value <<= 8;
	value |= str[0];

	return value;
}

rt_uint32_t str_to_uint32(rt_uint8_t *str)
{
	rt_uint32_t value = 0;

	value = str[0];
	value <<= 8;
	value |= str[1];
	value <<= 8;
	value |= str[2];
	value <<= 8;
	value |= str[3];

	return value;
}

rt_uint32_t time_to_sec(rt_uint8_t hour,rt_uint8_t min,rt_uint8_t sec)
{
	rt_uint32_t dsec;

	dsec = hour*3600;
	dsec += min*60;
	dsec += sec;

	return dsec;
}


void show_hex_string(rt_uint8_t *str,rt_uint16_t size)
{
#ifndef RemovDebugCode
	rt_uint16_t i;

	for(i=0;i<size;i++)
	{
		//rt_kprintf("\033[1m\033[40;33m%02X \033[0m",str[i]);
		#ifdef USING_RAW_ENC  //加密显示
		rt_kprintf("%02X ",(str[i]+rand())%0xff);
		#else
		rt_kprintf("%02X ",str[i]);
		#endif
	}
	rt_kprintf("\n");
#endif
}


void show_int_string(rt_uint8_t *str,rt_uint16_t size)
{
#ifndef RemovDebugCode
	rt_uint16_t i;

	for(i=0;i<size;i++)
	{
		rt_kprintf("%d ",str[i]);
	}
	rt_kprintf("\n");
#endif
}


void show_ch_string(rt_uint8_t *str,rt_uint16_t size)
{
#ifndef RemovDebugCode
	rt_uint16_t i;

	for(i=0;i<size;i++)
	{
		rt_kprintf("%c",str[i]);
	}
	rt_kprintf("\n");
#endif
}


rt_uint16_t net_crc16(rt_uint8_t *buf, rt_uint16_t len)
{
		rt_uint8_t j;
		rt_uint16_t i;
		rt_uint16_t a=0xaaaa;
		rt_uint16_t b=0xa001;
		
		for(i=0; i<len; i++)
		{
			a ^= buf[i];
			for(j=0;j<8;j++)
			{
				rt_uint16_t last;
				last = a % 2;
				a >>= 1;
				if(last == 1)
				{
					a ^= b;
				}
			}
		}
    return a;
} 


rt_uint8_t fun_check_sum(rt_uint8_t *i,rt_uint8_t ln) 
{ 
	rt_uint8_t j,tempq=0; i+=1; 
	
	for(j=0;j<(ln-2);j++) 
	{ 
		tempq+=*i; 
		i++; 
	}
	tempq=(~tempq)+1; 
	return(tempq); 
}

rt_uint8_t avd_check_sum(rt_uint8_t *i,rt_uint8_t ln) 
{ 
	rt_uint8_t j,tempq=0; 
	
	for(j=0;j<(ln-1);j++) 
	{ 
		tempq += (*i); 
		i++; 
	}
	//tempq=(~tempq)+1; 
	return(tempq); 
}


/* ----------------------------------------------------------------------------*/
/**
 * @brief InvertUint8       有符号数据转换
 *
 * @param DesBuf    目标缓存
 * @param SrcBuf    源缓存
 *
 * @return   void
 */
/* ----------------------------------------------------------------------------*/
void InvertUint8(rt_uint8_t *DesBuf, rt_uint8_t *SrcBuf)
{
    int i;
    rt_uint8_t temp = 0;
    
    for(i = 0; i < 8; i++)
    {
        if(SrcBuf[0] & (1 << i))
        {
            temp |= 1<<(7-i);
        }
    }
    DesBuf[0] = temp;
}

/* ----------------------------------------------------------------------------*/
/**
 * @brief InvertUint16      有符号2字节数据转换
 *
 * @param DesBuf     目标缓存
 * @param SrcBuf     源缓存
 *
 * @return  void 
 */
/* ----------------------------------------------------------------------------*/
void InvertUint16(rt_uint16_t *DesBuf, rt_uint16_t *SrcBuf)  
{  
    int i;  
    rt_uint16_t temp = 0;    
    
    for(i = 0; i < 16; i++)  
    {  
        if(SrcBuf[0] & (1 << i))
        {          
            temp |= 1<<(15 - i);  
        }
    }  
    DesBuf[0] = temp;  
}


/* ----------------------------------------------------------------------------*/
/**
 * @brief CRC16_CCITT       CCITT模型crc校验
 *
 * @param puchMsg      校验缓存
 * @param usDataLen    校验长度
 *
 * @return  校验值 
 */
/* ----------------------------------------------------------------------------*/
rt_uint16_t CRC16_CCITT(rt_uint8_t *puchMsg, rt_uint16_t usDataLen)  
{  
    rt_uint16_t wCRCin = 0x0000;  
    rt_uint16_t wCPoly = 0x1021;  
    rt_uint8_t wChar = 0;  
    
    while (usDataLen--)     
    {  
        wChar = *(puchMsg++);  
        InvertUint8(&wChar, &wChar);  
        wCRCin ^= (wChar << 8); 
        
        for(int i = 0; i < 8; i++)  
        {  
            if(wCRCin & 0x8000)
            {
                wCRCin = (wCRCin << 1) ^ wCPoly; 
            }            
            else 
            {              
                wCRCin = wCRCin << 1;  
            }
        }  
    }  
    InvertUint16(&wCRCin, &wCRCin);  
    return (wCRCin) ;  
} 

 
/* ----------------------------------------------------------------------------*/
/**
 * @brief CRC16_CCITT_FALSE       CCITT_FALSE模型crc校验
 *
 * @param puchMsg      校验缓存
 * @param usDataLen    校验长度
 *
 * @return   校验值 
 */
/* ----------------------------------------------------------------------------*/
rt_uint16_t CRC16_CCITT_FALSE(rt_uint8_t *puchMsg, rt_uint16_t usDataLen)  
{  
    rt_uint16_t wCRCin = 0xFFFF;  
    rt_uint16_t wCPoly = 0x1021;  
    rt_uint8_t wChar = 0;  
    
    while (usDataLen--)     
    {  
        wChar = *(puchMsg++);  
        wCRCin ^= (wChar << 8); 
        
        for(rt_uint16_t i = 0; i < 8; i++)  
        {  
            if(wCRCin & 0x8000)  
            {
                wCRCin = (wCRCin << 1) ^ wCPoly;  
            }
            else  
            {
                wCRCin = wCRCin << 1; 
            }            
        }  
    }  
    return (wCRCin) ;  
}  


/* ----------------------------------------------------------------------------*/
/**
 * @brief CRC16_XMODEM      XMODEM 模型crc校验
 *
 * @param puchMsg       校验缓存
 * @param usDataLen     校验长度
 *
 * @return  
 */
/* ----------------------------------------------------------------------------*/
rt_uint16_t CRC16_XMODEM(rt_uint8_t *puchMsg, rt_uint16_t usDataLen)  
{  
    rt_uint16_t wCRCin = 0x0000;  
    rt_uint16_t wCPoly = 0x1021;  
    rt_uint8_t wChar = 0;  
    
    while (usDataLen--)     
    {  
        wChar = *(puchMsg++);  
        wCRCin ^= (wChar << 8);
        
        for(rt_uint16_t i = 0; i < 8; i++)  
        {  
            if(wCRCin & 0x8000)  
            {
                wCRCin = (wCRCin << 1) ^ wCPoly;  
            }
            else
            {              
                wCRCin = wCRCin << 1;
            }
        }  
    }  
    return (wCRCin) ;  
}  
  

/* ----------------------------------------------------------------------------*/
/**
 * @brief CRC16_X25       X25模型crc校验
 *
 * @param puchMsg       校验缓存
 * @param usDataLen     校验长度
 *
 * @return  
 */
/* ----------------------------------------------------------------------------*/
rt_uint16_t CRC16_X25(rt_uint8_t *puchMsg, rt_uint16_t usDataLen)  
{  
    rt_uint16_t wCRCin = 0xFFFF;  
    rt_uint16_t wCPoly = 0x1021;  
    rt_uint8_t wChar = 0;  
    
    while (usDataLen--)     
    {  
        wChar = *(puchMsg++);  
        InvertUint8(&wChar, &wChar);  
        wCRCin ^= (wChar << 8); 
        
        for(rt_uint16_t i = 0;i < 8;i++)  
        {  
            if(wCRCin & 0x8000)
            {              
                wCRCin = (wCRCin << 1) ^ wCPoly; 
            }            
            else  
            {
                wCRCin = wCRCin << 1; 
            }            
        }  
    }  
    InvertUint16(&wCRCin, &wCRCin);  
    return (wCRCin^0xFFFF) ;  
}  
  

/* ----------------------------------------------------------------------------*/
/**
 * @brief CRC16_MODBUS      MODBUS模型crc校验
 *
 * @param puchMsg        校验缓存
 * @param usDataLen      校验长度
 *
 * @return  
 */
/* ----------------------------------------------------------------------------*/
rt_uint16_t CRC16_MODBUS(rt_uint8_t *puchMsg, rt_uint16_t usDataLen)  
{  
    rt_uint16_t wCRCin = 0xFFFF;  
    rt_uint16_t wCPoly = 0x8005;  
    rt_uint8_t wChar = 0;  
    
    while (usDataLen--)     
    {  
        wChar = *(puchMsg++);  
        InvertUint8(&wChar, &wChar);  
        wCRCin ^= (wChar << 8); 
        
        for(rt_uint16_t i = 0; i < 8; i++)  
        {  
            if(wCRCin & 0x8000) 
            {
                wCRCin = (wCRCin << 1) ^ wCPoly;  
            }
            else  
            {
                wCRCin = wCRCin << 1; 
            }            
        }  
    }  
    InvertUint16(&wCRCin, &wCRCin);  
    return (wCRCin) ;  
} 
 

/* ----------------------------------------------------------------------------*/
/**
 * @brief CRC16_IBM      IBM模型crc校验 
 *
 * @param puchMsg        校验缓存
 * @param usDataLen      数据长度
 *
 * @return  
 */
/* ----------------------------------------------------------------------------*/
rt_uint16_t CRC16_IBM(rt_uint8_t *puchMsg, rt_uint16_t usDataLen)  
{  
    rt_uint16_t wCRCin = 0x0000;  
    rt_uint16_t wCPoly = 0x8005;  
    rt_uint8_t wChar = 0;  
    
    while (usDataLen--)     
    {  
        wChar = *(puchMsg++);  
        InvertUint8(&wChar, &wChar);  
        wCRCin ^= (wChar << 8);  
        
        for(rt_uint16_t i = 0; i < 8; i++)  
        {  
            if(wCRCin & 0x8000)  
            {
                wCRCin = (wCRCin << 1) ^ wCPoly; 
            }            
            else  
            {
                wCRCin = wCRCin << 1;  
            }
        }  
    }  
    InvertUint16(&wCRCin,&wCRCin);  
    return (wCRCin) ;  
}  


/* ----------------------------------------------------------------------------*/
/**
 * @brief CRC16_MAXIM       MAXIM模型crc校验
 *
 * @param puchMsg
 * @param usDataLen
 *
 * @return  
 */
/* ----------------------------------------------------------------------------*/
rt_uint16_t CRC16_MAXIM(rt_uint8_t *puchMsg, rt_uint16_t usDataLen)  
{  
    rt_uint16_t wCRCin = 0x0000;  
    rt_uint16_t wCPoly = 0x8005;  
    rt_uint8_t wChar = 0;  
    
    while (usDataLen--)     
    {  
        wChar = *(puchMsg++);  
        InvertUint8(&wChar, &wChar);  
        wCRCin ^= (wChar << 8);  
        
        for(rt_uint16_t i = 0; i < 8; i++)  
        {  
            if(wCRCin & 0x8000) 
            {              
                wCRCin = (wCRCin << 1) ^ wCPoly;
            }
            else 
            {              
                wCRCin = wCRCin << 1;  
            }
        }  
    }  
    InvertUint16(&wCRCin, &wCRCin);  
    return (wCRCin^0xFFFF) ;  
}  


/* ----------------------------------------------------------------------------*/
/**
 * @brief CRC16_USB       USB模型crc校验
 *
 * @param puchMsg
 * @param usDataLen
 *
 * @return  
 */
/* ----------------------------------------------------------------------------*/
rt_uint16_t CRC16_USB(rt_uint8_t *puchMsg, rt_uint16_t usDataLen)  
{  
    rt_uint16_t wCRCin = 0xFFFF;  
    rt_uint16_t wCPoly = 0x8005;  
    rt_uint8_t wChar = 0;  
    
    while (usDataLen--)     
    {  
        wChar = *(puchMsg++);  
        InvertUint8(&wChar, &wChar);  
        wCRCin ^= (wChar << 8); 
        
        for(rt_uint16_t i = 0; i < 8; i++)  
        {  
            if(wCRCin & 0x8000) 
            {              
                wCRCin = (wCRCin << 1) ^ wCPoly;
            }            
            else  
            {
                wCRCin = wCRCin << 1; 
            }            
        }  
    }  
    InvertUint16(&wCRCin, &wCRCin);  
    return (wCRCin^0xFFFF) ;  
}


/* ----------------------------------------------------------------------------*/
/**
 * @brief kalman_filter       卡尔曼滤波算法
 *
 * @param ADC_Value    ADC值
 *
 * @return  滤波值 
 */
/* ----------------------------------------------------------------------------*/
unsigned long kalman_filter(kalmanFilterDef *filter, unsigned long ADC_Value,float Q)
{
	float x_k1_k1,x_k_k1;
  float Z_k;
  float kalman_adc;
  
	if(filter->initFlag == 0)//初始化参数，如果为1表示在外面初始化的
	{
		filter->initFlag = 1;
		filter->Q = Q;
		filter->R = 5;
		filter->Kg = 0;
		filter->P_k_k1 = 1;
		filter->kalman_adc_old = 0;
	}
	
  Z_k = ADC_Value;

  if (abs((int)(filter->kalman_adc_old -ADC_Value))>=10)
  { 
    x_k1_k1= ADC_Value*0.382 + filter->kalman_adc_old*0.618;
  }else
  {
    x_k1_k1 = filter->kalman_adc_old;
  }
  x_k_k1 = x_k1_k1;
  filter->P_k_k1 = filter->P_k1_k1 + filter->Q;

  filter->Kg = filter->P_k_k1/(filter->P_k_k1 + filter->R);

  kalman_adc = x_k_k1 + filter->Kg * (Z_k - filter->kalman_adc_old);
  filter->P_k1_k1 = (1 - filter->Kg)*filter->P_k_k1;
  filter->P_k_k1 = filter->P_k1_k1;

  filter->ADC_OLD_Value = ADC_Value;
  filter->kalman_adc_old = kalman_adc;

  return (rt_uint32_t)kalman_adc;
}

void string02_del_space(rt_uint8_t *str)
{
	rt_uint8_t *buf;
	rt_uint8_t i,j;
	rt_uint8_t size;

	buf = rt_calloc(1,rt_strlen((const char *)str));
	RT_ASSERT(buf);

	size = rt_strlen((const char *)str);
	j=0;
	for(i=0;i<size;i++)
	{
		if(str[i] != ' ')
		{
			buf[j++] = str[i];
		}
	}

	rt_memset(str,0,size);
	rt_strncpy((char *)str,(const char *)buf,j);

	rt_free(buf);
}


/* ----------------------------------------------------------------------------*/
/**
 * @brief string02_to_hex       将xx格式得字符串转换为十六进制数组
 *
 * @param buf  字符串
 * @param hex  16进制数组
 *
 * @return  字节个数
 */
/* ----------------------------------------------------------------------------*/
rt_uint16_t string02_to_hex(rt_uint8_t *buf,rt_uint8_t *hex)
{
	rt_uint16_t i;
	rt_uint16_t pos = 0;
	rt_uint16_t strLen;

	strLen = rt_strlen((const char *)buf);
	//rt_kprintf("strLen=%d\r\n",strLen);
	if(strLen%2 != 0)
	{
		strLen += 1;
	}
	for(i=0;i<(strLen/2);i++)
	{
		rt_uint8_t str[3] = {0,0,0};
		rt_strncpy((char *)str,(const char *)&buf[i*2],2);
		hex[pos++] = strtol((const char *)str, NULL, 16);
	}

	return pos;
}

//将十六进制数组转换为字符串
void hex_to_string02(rt_uint8_t *hex,rt_uint16_t size,rt_uint8_t *str)
{
	int i;
	
	for(i=0; i<size; i++)
	{
		rt_sprintf((char *)(str + rt_strlen((const char *)str)),"%02X ",hex[i]);
	}
}

#include <stdio.h>
#include <stdlib.h>
void generateRandomString(int length,char *randomString) 
{
    static const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    int i;
		
    for (i = 0; i < length; i++) 
		{
        int randomIndex = rand() % (sizeof(charset) - 1);
        randomString[i] = charset[randomIndex];
    }
    randomString[length] = '\0';
}

